package com.chain.BrotherExchange.ui.fragment;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;

import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.Resource;
import com.chain.BrotherExchange.data.model.QuotationTypes;
import com.chain.BrotherExchange.ui.adapter.SimpleFragmentPagerAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateFragment;
import com.chain.BrotherExchange.utils.ActivityBarHelper;
import com.chain.BrotherExchange.viewmodel.QuotationViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 行情
 */
public class QuotationFragment extends AbsNetworkStateFragment {

    @Bind(R.id.quotation_view_pager) ViewPager mQuotationViewPager;

    private QuotationViewModel mViewModel;

    public static Fragment newInstance() {
        return new QuotationFragment();
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_quotation;
    }

    @Override
    protected void initActions() {
        mViewModel = ViewModelProviders.of(this).get(QuotationViewModel.class);

        // 行情类型
        initQuotationTypes();
    }

    private void initQuotationTypes() {
        mViewModel.quotationTypes().observe(this, resource ->
                Resource.load(getContext(), resource, this, this::loadQuotationOntoView)
        );
    }

    private void loadQuotationOntoView(List<QuotationTypes.DataBean.ListBean> result) {
        List<String> pageTitles = new ArrayList<>();
        List<Fragment> fragments = new ArrayList<>();
        for (QuotationTypes.DataBean.ListBean bean : result) {
            pageTitles.add(bean.getMark());
            fragments.add(QuotationListFragment.newInstance(bean.getToken_id()));
        }

        mQuotationViewPager.setAdapter(new SimpleFragmentPagerAdapter(
                getFragmentManager(), fragments, pageTitles)
        );
    }


    @Override
    protected void onVisible() {
        ActivityBarHelper.setTransparentDarkColorStatus(getActivity());
    }

    @Override
    public void onAnewRequest() {
        initQuotationTypes();
    }
}
